﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace Numble
{
    class EndGame
    {
        static int size = 7;
        static int time = 1000;
        public static Random next = new Random();
        
        public static void End(int n, int time)
        {
            // printing E
            int heigth = n;
            int column = n + 5;
            PrintE(n, heigth, column);

            ////print N
            heigth += n;
            column += 0;
            PrintN(n, heigth, column);

            //// printing D
            heigth += n;
            column += 0;
            PrintD(n, heigth, column);
            Thread.Sleep(2 * time);

        }

        public static void PointsAtTheEnd(int points, int n, int time)
        {
            Dictionary<string, int> highScore = new Dictionary<string, int>();
            ReadCurrentHighScore(highScore);

            if (highScore.Count == 0 || highScore.Count < 5 || IsItInTopFive(highScore, points))
            {
                if (highScore.Count == 0 || TheBestResult(highScore, points))
                {
                    WriteOnConsoleGamer(points, "YOU ARE THE BEST NUMBLER :)");
                }
                else
                {
                    Console.SetCursorPosition(0, 15);
                    WriteOnConsoleGamer(points, "   YOU ARE IN TOP 5 :)");
                }
                WriteOtFile(highScore, points);
            }
            else
            {
                NotBestScoreMesage();
            }

        }

        private static void NotBestScoreMesage()
        {
            Console.Clear();
            Console.SetCursorPosition(5, 13);
            Console.Write("SORRY THIS IS NOT THE BEST SCORE :(");

            Console.SetCursorPosition(15, 15);
            Console.Write("TRY AGAIN");

            Console.SetCursorPosition(17, 28);
            Console.Write("Press any key...");
            Console.ReadLine();
            Console.SetCursorPosition(21, 28);//

        }

        private static void WriteOtFile(Dictionary<string, int> highScore, int points)
        {
            string name = AskForName();
            highScore[name] = points;

            var result = highScore.OrderByDescending(x => x.Value);
            int count = 0;

            int high = 5;

            StreamWriter score = new StreamWriter(@"score.sc");
            Console.Clear();
            using (score)
            {
                foreach (var item in result)
                {
                    if (count == 5)
                    {
                        score.WriteLine("EOF");
                        break;
                    }
                    else
                    {
                        Console.SetCursorPosition(10, high++);//high++
                        Console.Write(count + 1 + " " + item.Key + " " + item.Value);
                        score.WriteLine(item.Key + "-" + item.Value);
                        count++;
                    }
                }
            }
            Console.SetCursorPosition(17, 28);
            Console.Write("Press any key...");
            Console.ReadLine();
            Console.SetCursorPosition(21, 28);//

        }

        private static string AskForName()
        {
            string name;
            do
            {
                Console.Clear();
                Console.SetCursorPosition(0, 13);
                Console.WriteLine("     PLEASE ENTER YOUR NAME: ");
                Console.SetCursorPosition(13, 15);
                name = Console.ReadLine().Trim();

            } while (name.Length < 1);

            return name;
        }

        private static void WriteOnConsoleGamer(int points, string message)
        {
            Console.Clear();
            Console.SetCursorPosition(3, 13);
            Console.WriteLine(message);         //print message

            Console.SetCursorPosition(15, 15);
            Console.WriteLine(points);          // print high score

            Thread.Sleep(3000);
            // NameGame.Name(n, time);             // Little animation
            //Thread.Sleep(1000);

        }


        private static bool TheBestResult(Dictionary<string, int> highScore, int points)
        {
            foreach (var item in highScore)
            {
                if (item.Value >= points)
                {
                    return false;
                }
            }
            return true;
        }

        private static bool IsItInTopFive(Dictionary<string, int> highScore, int points)
        {
            foreach (var item in highScore)
            {
                if (item.Value < points)
                {
                    return true;
                }
            }
            return false;
        }

        private static void ReadCurrentHighScore(Dictionary<string, int> highScore)
        {
            if (File.Exists(@"score.sc"))
            {
                StreamReader score = new StreamReader(@"score.sc");
                using (score)
                {
                    string currLine = score.ReadLine();
                    while (currLine != "EOF" && currLine != null)
                    {
                        var input = currLine.Split('-');
                        highScore[input[0]] = int.Parse(input[1]);
                        currLine = score.ReadLine();
                    }
                }
            }

        }

        static void PrintE(int n, int heigth, int column)
        {

            string[,] matrix = new string[n, (n - 2)];
            Console.ForegroundColor = ConsoleColor.Green;

            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                Console.SetCursorPosition(heigth, column++);

                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if (row == 0 || row == n / 2 || row == n - 1)
                    {
                        int num = next.Next(0, 10);
                        Console.Write(num);
                    }
                    else
                    {
                        Console.Write(" ");
                    }

                }

                Console.WriteLine();
            }

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine();
        }

        private static void PrintN(int n, int heigth, int column)
        {
            string[,] matrix = new string[n, (n - 2)];
            Console.ForegroundColor = ConsoleColor.Yellow;

            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                Console.SetCursorPosition(heigth, column++);

                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if (col == 0 || col == matrix.GetLength(1) - 1 || row - col == 1)
                    {
                        int num = next.Next(0, 10);
                        Console.Write(num);
                    }
                    else
                    {
                        Console.Write(" ");
                    }

                }
                Console.WriteLine();
            }

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine();
        }

        private static void PrintD(int n, int heigth, int column)
        {
            string[,] matrix = new string[n, (n - 2)];
            Console.ForegroundColor = ConsoleColor.Red;

            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                Console.SetCursorPosition(heigth, column++);

                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if (col - row == (n - 2) / 2 || col == 0 ||
                        (col == matrix.GetLength(1) - 1 && row >= (n / 2) - 1 && row <= (n - n / 2)) ||
                        (row + col == n - 1 + (n - 2) / 2) ||
                        ((row == 0 || row == matrix.GetLength(0) - 1) && col < matrix.GetLength(1) - 3))
                    {
                        int num = next.Next(0, 10);
                        Console.Write(num);
                    }
                    else
                    {
                        Console.Write(" ");
                    }

                }
                Console.WriteLine();
            }

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine();
        }
    }
}
